# -*- coding: UTF-8 -*-
import urllib
from urllib import request
import re
import time
import pymysql

from test.MyThreadPool import ThreadPool


# 下载进度
def reporthook(a, b, c, number, apkName):
    '''''回调函数
        @a:已经下载的数据块
        @b:数据块的大小
        @c:远程文件的大小
        '''
    per = 100.0 * a * b / c
    if per > 100:
        print('\033下载完毕')
        # 存数据
        # print('%.2f%%' % per)


def callback(status, result):
    """
    根据需要进行的回调函数，默认不执行。
    :param status: action函数的执行状态
    :param result: action函数的返回值
    :return:
    """
    # print("action函数的执行状态%saction函数的返回值%s" % status % result)
    pass


# 获取网页内容
def getHtml(count, arg):
    print("第%s个任务调用了线程 %s，并打印了这条信息！" % (arg + 1, count))
    page = urllib.request.urlopen("https://www.apk3.com/soft/%s.html" % arg)
    html = page.read()
    html = html.decode('gb2312', 'ignore')
    getApk(html, count)


# 解析网页内容
def getApk(html, i=0):
    typelocation = re.search(r'安卓软件下载网</a> → <a href="/game/">安卓游戏</a>', html)
    if typelocation != None:
        apkUrlList = re.findall('href="(http.*?\.apk)"', html)
        apkSizeList = re.findall('(\d+\.\d+)MB', html)
        appName = re.findall('<div class="a_app_attr_tit">\n<h1>(.*?)</h1>', html)
        # print(
        #     'apkSizeList %s>>>apkUrlList %s>>> apkUrlList.__len__()%s ' % apkSizeList % apkUrlList % apkUrlList.__len__())
        # print('appName||', appName[0] if appName.__len__() > 0 else '>><<<')
        if apkSizeList.__len__() > 0:
            if apkUrlList.__len__() > 0 and (float(apkSizeList[0])) < 100.0:
                print('正在下载', apkUrlList[0])
                urllib.request.urlretrieve(apkUrlList[0],
                                           'E:/app/apk/%s.apk' % (appName[0] if appName.__len__() > 0 else str(i))
                                          )


# 查询上次最后一次的序号
def selectNo(cursor):
    cursor.execute("select *  from apk3 where _id=(select max(_id)  from apk3)")
    maxNO = cursor.fetchall()
    if maxNO.__len__() > 0:
        firstNo = maxNO[0][1]
        return firstNo
    return 0


# 插入序号
def installNO(cursor, db, NO, appName="", downloadTime="", TIME=0):
    sql = "insert into apk3(NO,appName, downloadTime, TIME) values (%s,%s,%s, %s)" % (NO, appName, downloadTime, TIME)
    cursor.execute(sql)
    db.commit()
    db.rollback()

# http://gyxz.hwm6b6.cn/a31/yx_wulan1/wxckfzjh.apk
# https://www.apk3.com/soft/277982.html,
if __name__ == '__main__':
    db = pymysql.connect("localhost", "root", "164531", "appmsg")
    cursor = db.cursor()
    firstNo = selectNo(cursor)
    pool = ThreadPool(2)
    firstNo = 277982
    for i in range(firstNo, 277999):
        print('抓取第', i, '页面')
        pool.put(getHtml, (i,))
    # 等待一定时间，让线程执行任务
    time.sleep(1)
    print("-" * 50)
    # print("\033[32;0m任务停止之前线程池中有%s个线程，空闲的线程有%s个！\033[0m" % (len(pool.generate_list), len(pool.free_list)))
    # 正常关闭线程池
    # pool.close()
    print("任务加载完毕，等待下载完成！")
